# -*- coding: utf-8 -*-            
# @Time : 2024/9/20 13:46
# @FileName: 01.py
# @Target:
import json, codecs, time, os
from pprint import pprint
import requests
from tqdm import tqdm

# with codecs.open(filename='00-csp_label.json', mode='r',
#                  encoding='utf-8') as fr:
#     D = json.load(fr)
# ZH_dict = {}
# EN_dict = {}
# SET = set()
# for KEY, VALUE in D.items():
#     for key, value in VALUE.items():
#         ZH = value[1]
#         EN = value[2]
#         for zh in ZH:
#             csp_1 = zh['csp_1']
#             if csp_1 not in ZH_dict:
#                 ZH_dict.update({csp_1 : {}})
#             csp_2 = zh['csp_2']
#             if csp_2 not in ZH_dict[csp_1]:
#                 ZH_dict[csp_1].update({csp_2 : {}})
#             csp_3 = zh['csp_3']
#             if csp_3 not in ZH_dict[csp_1][csp_2]:
#                 ZH_dict[csp_1][csp_2].update({csp_3 : []})
#             csp_4 = zh['csp_4']
#             if len(csp_4):
#                 if csp_4 not in ZH_dict[csp_1][csp_2][csp_3]:
#                     ZH_dict[csp_1][csp_2][csp_3].append(csp_4)
#             SET.add(csp_4)
#             SET.add(csp_3)
#             SET.add(csp_2)
#             SET.add(csp_1)
#
#
# class Translator:
#     def __init__(self):
#         self.url = "http://152.32.135.145:17310/translate"
#
#     def translate(self, sentence):
#         '''
#         use r'\n' symbol to split the sentence
#         the target customer voice has already remove r'\n'
#         '''
#         data = {
#             'sentence': sentence,
#         }
#         res = requests.post(
#             url=self.url, data=data
#         )
#         result = json.loads(res.content.decode('unicode_escape'))['trans_result']
#         return result
#
# translator = Translator()
# SET = set()
#
# with codecs.open(filename='csp-labels.json', mode='r', encoding='utf-8') as fr:
#     D = json.load(fr)
#
# def callback(DICT):
#     for k, v in DICT.items():
#         SET.add(k)
#         if isinstance(v, dict):
#             callback(v)
#         if isinstance(v, list):
#             for _ in v:
#                 SET.add(_)
# callback(D)
# SET = list(SET)
# print(SET)
# SET.remove('')
# DICT = {}
# for K in tqdm(SET):
#     dst = translator.translate(K)[0]['dst']
#     DICT.update({ K : dst })
#     time.sleep(0.1)
#
# with codecs.open(filename='csp-labels-en.json', mode='w', encoding='utf-8') as fw:
#     json.dump(obj=DICT, fp=fw, ensure_ascii=False, indent=4)
#
# print(SET)

# print(ZH_dict)
# SET = list(SET)
#
# for i in range(len(SET)):
#     if SET[i] != '':
#         EN_dict.update({SET[i] : translator.translate(SET[i])[0]['dst']})
#
# with codecs.open(filename='csp-labels.json', mode='w', encoding='utf-8') as fw:
#     json.dump(obj=ZH_dict, fp=fw, ensure_ascii=False, indent=4)
#
# with codecs.open(filename='csp-labels-en.json', mode='w', encoding='utf-8') as fw:
#     json.dump(obj=EN_dict, fp=fw, ensure_ascii=False, indent=4)
#

#
# with codecs.open(filename='01-csp-labels.json', mode='r', encoding='utf-8') as fr:
#     D = json.load(fr)
# new_D = {
#     '智驾': {},
#     '部件': {},
#     '交互': {},
#     '服务': {},
#     '互联': {},
#     '安全': {}
# }
# zhijia = D['智驾']
# for L, VALUE in zhijia.items():
#     for k, v in VALUE.items():
#         if k not in new_D['智驾'].keys():
#             new_D['智驾'].update({k : []})
#         new_D['智驾'][k].extend(v)
# print(D.keys()) # ['智驾', '部件', '交互', '服务', '互联', '系统', '安全']
# new_D['部件'] = D['部件']
# new_D['交互'] = D['交互']
# new_D['服务'] = D['服务']
# new_D['互联'] = D['互联']
# new_D['电池电机电控系统'] = D['电池电机电控系统']
# new_D['安全'] = D['安全']
# pprint(new_D)
#
# with codecs.open(filename='csp-labels.json', mode='w', encoding='utf-8') as fw:
#     json.dump(obj=new_D, fp=fw, ensure_ascii=False, indent=4)
#
#



# with codecs.open(filename='csp-labels.json', mode='r', encoding='utf-8') as fr:
#     D = json.load(fr)
#
# zhijia = D['智驾']
# DICT = {}
# for key , value in zhijia.items():
#     if key not in DICT.keys():
#         DICT.update({key : {}})
#     for v in value:
#         DICT[key][v] = []
#
# D['智驾'] = DICT
#
# pprint(D)
#
# with codecs.open(filename='csp-labels.json', mode='w', encoding='utf-8') as fw:
#     json.dump(obj=D, fp=fw, ensure_ascii=False, indent=4)
#
#





#
# with codecs.open(filename='feeling-labels.json', mode='r', encoding='utf-8') as fr:
#     D = json.load(fr)
#
# class Translator:
#     def __init__(self):
#         self.url = "http://152.32.135.145:17310/translate"
#
#     def translate(self, sentence):
#         '''
#         use r'\n' symbol to split the sentence
#         the target customer voice has already remove r'\n'
#         '''
#         data = {
#             'sentence': sentence,
#         }
#         res = requests.post(
#             url=self.url, data=data
#         )
#         result = json.loads(res.content.decode('unicode_escape'))['trans_result']
#         return result
#
# translator = Translator()
# SET = set()
#
# for key, value in D.items():
#     SET.add(key)
#     for v in value:
#         SET.add(v)
#
# SET = list(SET)
# DICT = {}
#
# for s in SET:
#     dst = translator.translate(s)[0]['dst']
#     DICT.update({s : dst})
#
# with codecs.open(filename='feeling-labels-en.json', mode='w', encoding='utf-8') as fw:
#     json.dump(obj=DICT, fp=fw, ensure_ascii=False, indent=4)
#
#


# 直接把 CSP 的一级标签省去，直接从二级标签开始做级标签
# with codecs.open(filename='csp-labels.json', mode='r', encoding='utf-8') as fr:
#     D = json.load(fr)
# new_D = {}
# for KEY, VALUE in D.items():
#     new_D.update(VALUE)
#
# with codecs.open('csp-labels.json', mode='w', encoding='utf-8') as fw:
#     json.dump(obj=new_D, fp=fw, ensure_ascii=False, indent=4)
#



# 20250212 : 对所有的标签体系做一下重新整理
# with codecs.open(filename='old-2024/csp-labels.json',
#                  mode='r', encoding='utf-8') as fr:
#     D = json.load(fr)
# new_csp = {}
# for KEY, VALUE in D.items():
#     if KEY not in new_csp.keys():
#         new_csp.update({KEY : []})
#     for key, value in VALUE.items():
#         if len(value)==0:
#             new_csp[KEY].append(key)
#         else:
#             for v in value:
#                 new_csp[KEY].append(key + '-' + v)
#
# with codecs.open(filename='csp_labels_20250212.json',
#                  mode='w', encoding='utf-8') as fw:
#     json.dump(obj=new_csp, fp=fw, ensure_ascii=False, indent=4)

# 然后将CSP 对应的翻译转换
# with codecs.open(filename='old-2024/csp-labels-en.json',
#                  mode='r', encoding='utf-8') as fr:
#     trans = json.load(fr)
#
# with codecs.open(filename='csp_labels_20250212.json',
#                  mode='r', encoding='utf-8') as fr:
#     D = json.load(fr)
# new_trans = {}
# for KEY, VALUE in D.items():
#     if KEY in trans.keys():
#         new_trans.update({KEY : trans[KEY]})
#     else:
#         new_trans.update({KEY : ''})
#     for V in VALUE:
#         if V in trans.keys():
#             new_trans.update({V : trans[V]})
#         else:
#             if '-' in V:
#                 V_1 = V.split('-')[0]
#                 V_2 = V.split('-')[1]
#                 if V_1 in trans.keys() and V_2 in trans.keys():
#                     new_trans.update({V : trans[V_1] + '-' + trans[V_2]})
#                 else:
#                     new_trans.update({V : ''})
#
# with codecs.open(filename='01.json',
#                  mode='w', encoding='utf-8') as fw:
#     json.dump(obj=new_trans, fp=fw, ensure_ascii=False, indent=4)


# with codecs.open(filename='csp_labels_20250212.json',
#                  mode='r', encoding='utf-8') as fr:
#     D = json.load(fr)
#
# with codecs.open(filename='csp_labels_en_20250212.json', mode='r',
#                  encoding='utf-8') as fr:
#     trans = json.load(fr)
#
# new_D = {}
#
# for KEY, VALUE in D.items():
#     assert  KEY in trans.keys()
#     for V in VALUE:
#         assert V in trans.keys()
#



# 接着处理CuSa 相关的标签体系
# with codecs.open(filename='cuzu_labels_20250212.json', mode='r', encoding='utf-8') as fr :
#     D = json.load(fr)
# with codecs.open(filename='cuzu_labels_trans_20250212.json.json', mode='r', encoding='utf-8') as fr:
#     trans = json.load(fr)
#
# all_data = []
# for KEY, VALUE in D.items():
#     for V in VALUE:
#         label_1 = KEY
#         label_2 = V
#         label_1_en = trans[label_1]
#         label_2_en = trans[label_2]
#         all_data.append({'label_1': label_1,
#                           'label_2': label_2,
#                           'label_1_en': label_1_en,
#                           'label_2_en': label_2_en})
#
# import json
#
# import xlwt
#
# a = all_data
# title = list(set([j for i in a for j in i]))
# book = xlwt.Workbook()
# sheet = book.add_sheet('Sheet1', cell_overwrite_ok=True)  # 添加一个sheet页
# for i in range(len(title)):  # 循环列
#     sheet.write(0, i, title[i])  # 将title数组中的字段写入到0行i列中
# for i, it in enumerate(a):
#     for j, k in enumerate(title):
#         sheet.write(1 + i, j, it[k])
#
# book.save('haha.xls')
#


# 接着处理quality 对应的标签体系统。

# 对应的情感的标签体系
# D = [
#     '产品体验超出用户期望',
#     '产品体验满足或略高于用户期望',
#     '产品体验达到用户基本要求，没有显著的优点或者缺点',
#     '产品体验未满足用户期望',
#     '产品体验显著不符合用户期望',
# ]
# with codecs.open(filename='sentiment_labels_20250212.json',
#                  mode='w', encoding='utf-8') as fw:
#     json.dump(obj=D, fp=fw, ensure_ascii=False, indent=4)
#



# 区分论坛中客户描述
# D = [
#     '车辆质量问题：用户描述车辆的某个部件或功能出现了故障、损坏或不符合预期的情况',
#     '车辆使用体验感受：用户分享在使用车辆时的个人感受，包括舒适性、操控性、性能、使用成本等方面',
#     '广告内容：用户或经销商发布的内容，主要目的是推广车辆或相关产品，可能包含价格、优惠、促销等信息',
#     '车辆改装和个性化：用户分享车辆改装的经验和个性化配置，包括外观改装、性能提升等',
#     '车辆比较和推荐：用户对不同车型进行比较，分享购车建议和推荐',
#     '车辆社区和活动：用户分享参加车辆社区活动、车主聚会等信息',
#     '车辆历史和背景：用户分享车辆的历史信息、购买背景或使用经历',
#     '车辆技术讨论：用户讨论车辆的技术细节、新技术应用等',
#     '其他：不符合上述任何一类的其他内容，可能包括闲聊、无关内容等',
# ]
# with codecs.open(filename='forum_labels_20250212.json',
#                  mode='w', encoding='utf-8') as fw:
#     json.dump(obj=D, fp=fw, ensure_ascii=False, indent=4)
#


# 校检对应的中文和英文之间是完全对应的
with codecs.open(filename='cuzu_labels_20250212.json', mode='r', encoding='utf-8') as fr:
    D = json.load(fr)
with codecs.open(filename='cuzu_labels_trans_20250212.json', mode='r', encoding='utf-8') as fr:
    trans = json.load(fr)
for KEY, VALUE in D.items():
    if KEY in trans.keys():
        ...
    else:
        print(KEY)
    for V in VALUE:
        if V in trans.keys():
            ...
        else:
            print(V)















